package com.zzx.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzx.boot.common.BaseContext;
import com.zzx.boot.common.R;
import com.zzx.boot.pojo.AddressBook;
import com.zzx.boot.pojo.OrderDetail;
import com.zzx.boot.pojo.Orders;
import com.zzx.boot.pojo.ShoppingCart;
import com.zzx.boot.service.AddressBookService;
import com.zzx.boot.service.OrderDetailService;
import com.zzx.boot.service.OrderService;
import com.zzx.boot.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private AddressBookService addressBookService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功!");
    }

    @GetMapping("/userPage")
    public R<IPage> page(Integer page,Integer pageSize){
        IPage<Orders> page1 = orderService.page(page, pageSize);
        return R.success(page1);
    }
    @GetMapping("/page")
    public R<IPage> pageR(Integer page, Integer pageSize, String number, String beginTime,String endTime){
        Page<Orders> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
        //判断订单号不为空
        queryWrapper.eq(StringUtils.isNotEmpty(number),Orders::getNumber,number);
        queryWrapper.ge(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime);
        queryWrapper.le(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime);
        Page<Orders> page2 = orderService.page(page1, queryWrapper);
        return R.success(page2);
    }

    @PutMapping
    public R<String> order(@RequestBody Orders orders){
//        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
//        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
//        //查询用户地址
//        List<AddressBook> list = addressBookService.list(queryWrapper);
//        orders.setConsignee(list.get(0).getConsignee());
        boolean b = orderService.updateById(orders);
        return b?R.success("开始派送"):R.error("更改失败");
    }

    @PostMapping("/again")
    @Transactional
    public R<String> again(@RequestBody Orders orders){
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetail = orderDetailService.list(queryWrapper);
        //添加到购物车中
        for (OrderDetail detail : orderDetail) {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(detail,shoppingCart,"id");
            shoppingCart.setUserId(BaseContext.getCurrentId());
            boolean save = shoppingCartService.save(shoppingCart);
        }


        return R.success("添加成功!");
    }
}
